package com.hmsg.health.service;



import com.hmsg.health.bean.Dto.UserDto;
import lombok.extern.slf4j.Slf4j;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.convert.RemoveIndexedData;
import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

import com.hmsg.health.bean.domain.UserAccountDo;
import com.hmsg.health.bean.domain.UserAccountRecordDo;
import com.hmsg.health.bean.domain.UserDo;
import com.hmsg.health.bean.domain.UserTaskDo;
import com.hmsg.health.dao.UserAccountDao;
import com.hmsg.health.dao.UserAccountRecordDao;
import com.hmsg.health.dao.UserDao;
import com.hmsg.health.dao.UserTaskDao;

@Slf4j
@Service
public class UserService {
   
    @Autowired
    private UserDao userDao;
    @Autowired
    private UserTaskDao userTaskDao;
    @Autowired
    private UserAccountDao userAccountDao;
    @Autowired
    private UserAccountRecordDao userAccountRecordDao;
    public UserDo findOne(Integer id){
    	UserDo demoDo = userDao.select(id);
    	
        return demoDo == null ? null : demoDo;
    }
    
    public UserAccountDo findUserAccount(Integer id){
    	UserAccountDo demoDo = userAccountDao.select(id);
    	
        return demoDo == null ? null : demoDo;
    }
    
    public int updateUserAccount(UserAccountDo info){
    	Integer demoDo = userAccountDao.update(info);
    	
        return demoDo == null ? null : demoDo;
    }
    public UserAccountDo findUserAccountByUserId(Integer userId){
    	UserAccountDo demoDo = userAccountDao.selectByUserId(userId);
    	
        return demoDo == null ? null : demoDo;
    }
    
    public List<UserTaskDo> findAllUserTask(UserTaskDo info){
    	List<UserTaskDo> demoDo = userTaskDao.select(info);
    	
        return demoDo == null ? null : demoDo;
    }
    
    
    public UserTaskDo findOneUserTask(Integer id){
    	UserTaskDo demoDo = userTaskDao.findOne(id);
    	
        return demoDo == null ? null : demoDo;
    }

    public Integer save(UserDo user){
    	Integer res = userDao.save(user);
        return res;
    }
    
    public Integer saveUserAccount(UserAccountDo ua){
    	Integer res = userAccountDao.save(ua);
        return res;
    }
    
    public Integer delUserTask(Integer id){
    	Integer res = userTaskDao.delete(id);
        return res;
    }
    
    
    public Integer saveUserTask(UserTaskDo ut){
    	Integer res = userTaskDao.save(ut);
        return res;
    }
    
    public Integer updateUserTask(UserTaskDo ut){
    	Integer res = userTaskDao.update(ut);
        return res;
    }
    
    public Integer saveAccountRecord(UserAccountRecordDo info){
    	Integer res = userAccountRecordDao.save(info);
        return res;
    }
    
    public UserAccountRecordDo findOneAcountRecord(Integer id){
    	UserAccountRecordDo res = userAccountRecordDao.findOne(id);
        return res;
    }
    
    public List<UserTaskDo> selectUserTask(UserTaskDo ut){
    	List<UserTaskDo> res = userTaskDao.select(ut);
        return res;
    }
    

    public UserDo findByMobile(String mobile){
    	
    	UserDo demoDo = userDao.selectByMobile(mobile);
    	
        return demoDo == null ? null : demoDo;
    }
    
    public List<UserTaskDo> orderTaking(UserTaskDo ut){
    	
    	List<UserTaskDo> demoDo = userTaskDao.select(ut);
    	
        return demoDo == null ? null : demoDo;
    }
    
    public List<UserAccountRecordDo> selectAccountRecords(UserAccountRecordDo ut){
    	
    	List<UserAccountRecordDo> demoDo = userAccountRecordDao.select(ut);
    	
        return demoDo == null ? null : demoDo;
    }

    public List<UserDo> findAll(String phone,String unique,String time,
                                Integer firstResult, Integer maxResult){
        List<UserDo> userAll=userDao.findAll(phone,unique,time,firstResult,maxResult);
        if(userAll != null && userAll.size()>0){
            for(UserDo u:userAll) {
               UserAccountDo userAccount= userAccountDao.findUserAccount(u.getId());
               u.setUa(userAccount);
            }
        }
        return userAll == null ? null : userAll;
    }
    

    @Transactional
    public int update(UserDo user){
    	
    	return userDao.update(user);
    	
    }
    
    public int delete(Integer userId){
    	
    	return userDao.delete(userId);
    	
    }
    
   
}